/**
 * Copyright (c) 2020 rxi
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the MIT license. See `log.c` for details.
 */

#ifndef CLOG_H
#define CLOG_H

#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <time.h>

typedef struct {
  va_list ap;
  const char *fmt;
  const char *file;
  struct tm *time;
  void *udata;
  int line;
  int level;
} ClogEvent;

typedef void (*clogLogFn)(ClogEvent *ev);
typedef void (*clogLockFn)(bool lock, void *udata);

enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL };

#define ctrace(...) clog(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__)
#define cdebug(...) clog(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
#define cinfo(...) clog(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
#define cwarn(...) clog(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__)
#define cerror(...) clog(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
#define cfatal(...) clog(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__)

void clogSetLock(clogLockFn fn, void *udata);
void clogSetLevel(int level);
void clogSetQuiet(bool enable);
int clogAddCallback(clogLogFn fn, void *udata, int level);
int clogAddFp(FILE *fp, int level);

void clog(int level, const char *file, int line, const char *fmt, ...);

#endif
